package level.editor.menu;

import javax.swing.JOptionPane;

import level.editor.command.Command;
import level.editor.command.RequiresModule;
import level.editor.command.RequiresUpdate;
import level.editor.mediator.Module;
import level.editor.state.StateModule;
import level.state.StateReader;

public class StateNewCommand extends Command implements RequiresUpdate,
		RequiresModule
{
	public StateNewCommand()
	{
		super("StateModule");
	}

	public void execute(Module m)
	{
		StateModule module = (StateModule) m;
		if (module.isModified())
		{
			int response = JOptionPane
					.showConfirmDialog(
							module.getWindow(),
							"The current file "
									+ " has not been saved. Do you want to save this file?",
							"Unsaved file", JOptionPane.YES_NO_CANCEL_OPTION,
							JOptionPane.WARNING_MESSAGE);
			if (response == JOptionPane.CANCEL_OPTION)
				return;
			else if (response == JOptionPane.YES_OPTION)
			{
				module.pushDownCommand(new StateSaveCommand());
			}
		}

		StateReader.initiateBlankState(module.getState(), null);
		module.setModified(false);
		module.resetMemento();
	}
}
